/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JTable;
import util.JTableUtil;

/**
 *
 * @author Kuchcik
 */
public class Drugs {
    
    static String[] selectedRow;
    static JFrame parent;
    public Drugs(JFrame frame){
     this.parent=frame;
    }
    
    public static String[] getProducersList(){
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("Select id,nazwa from producenci_lekow");
            ResultSet rs = stmn.getResultSet();
            
            int n=SqlConnection.getRowCount(rs);
            String[] data = new String[n];
            rs.next();
            for(int i=0;i<n;i++){
                data[i]=rs.getString(1)+" "+rs.getString(2);
                rs.next();
            }
            return data;
            
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd pobierania listy producentów", true,parent);
        }
        return null;
    }
    
    public static JTable getAllDrugs(){
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("select L.id as ID, PL.nazwa as Producent, L.opis as Opis, L.refundacja as Refundowany, L.cena as Cena"
                    + " from leki as L join producenci_lekow as PL on L.id_producenta=PL.id");
            ResultSet rs = stmn.getResultSet();
            return JTableUtil.createTable(rs);
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
        return null;
    }
    
    public static JTable getDrugs(LinkedList<Integer> id)
    {
        try{
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            String query ="select L.id as ID, PL.nazwa as Producent, L.opis as Opis, L.refundacja as Refundowany, L.cena as Cena"
                    + " from leki as L join producenci_lekow as PL on L.id_producenta=PL.id where ";
            Iterator<Integer> it = id.iterator();
            while(it.hasNext())
            {
                Integer tmp = it.next();
                query += "L.id=" + tmp.toString();
                if(it.hasNext()) {
                    query += " OR ";
                }
            }
            if(id.size()==0)
                query  += "1=2";
            stmn.execute(query);
            ResultSet rs = stmn.getResultSet();
            return JTableUtil.createTable(rs);
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
        return null;
    }
    
    public static JTable getDrugs(String name, String producer, float price_from, float price_to, String ref){
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            String query ="select L.id as ID, PL.nazwa as Producent, L.opis as Opis, L.refundacja as Refundowany, L.cena as Cena"
                    + " from leki as L join producenci_lekow as PL on L.id_producenta=PL.id where ";
            boolean isfirstWhere=true;
            if(name!=null){
            isfirstWhere=false;
            query+="L.opis like '%"+name+"%'";
            }
            
            if(isfirstWhere==false && producer!=null) query+=" and ";
            
            if(producer!=null){
            isfirstWhere=false;
            query+="L.id_producenta="+producer;
            }
            
            if(isfirstWhere==false) query+=" and ";
            
            query+= "L.cena between "+price_from+" and "+price_to;
            
            if(isfirstWhere==false) query+=" and ";
             
            query+= "L.refundacja=";
            if(ref.equals("Tak")) query+="1"; else query+="0";
            
            System.out.println(query);
            stmn.execute(query);
            ResultSet rs = stmn.getResultSet();
            return JTableUtil.createTable(rs);
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
        return null;
    }
    
    public static void setSelectedRow(String[] row){
        Drugs.selectedRow=row;
    }
    
    public static  String[] getSelectedRow(){
        return Drugs.selectedRow;
    }
    
    public static boolean deleteMedicine(){
            try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("delete from leki where id="+selectedRow[0]);
            return true;
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Nie można usunąć", true,parent);
            return false;
        }
    }
    
    public static boolean addMedicine(String[] row){
            try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("insert into leki values("+row[0]+",'"+row[1]+"',"+row[2]+","+row[3]+")");
            return true;
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Nie można dodać(uzupełnij wszystkie miejsca)", true,parent);
            return false;
        }
    }    
}
